import os
import glob
from pdfminer.high_level import extract_pages
from pdfminer.layout import LTTextContainer


class TextExtractionTool:
    def __init__(self):
        pass

    @staticmethod
    def extract_text_from_pdf(filename, page_numbers=None, min_line_length=1):
        """
        从 PDF 文件中（按指定页码）提取文字
        filename: PDF 文件路径名
        page_numbers: 页码范围，默认全部页
        min_line_length: 最小行长度，用于过滤较短的文本行，默认 1
        """
        paragraphs = []  # 存储最终提取的段落的列表
        buffer = ''  # 用于暂时存储正在构建的段落
        full_text = ''  # 存储从PDF中提取的所有文本
        # 提取全部文本
        for i, page_layout in enumerate(extract_pages(filename)):
            # 如果指定了页码范围，跳过范围外的页
            if page_numbers is not None and i not in page_numbers:
                continue
            for element in page_layout:
                if isinstance(element, LTTextContainer):
                    full_text += element.get_text() + '\n'
        # 按空行分隔，将文本重新组织成段落
        lines = full_text.split('\n')
        for text in lines:
            if len(text) >= min_line_length:
                buffer += (' ' + text) if not text.endswith('-') else text.strip('-')
            elif buffer:
                paragraphs.append(buffer)
                buffer = ''
        if buffer:
            paragraphs.append(buffer)
        return paragraphs

    @staticmethod
    def extract_text_from_txt(folder_path, file_pattern="*.md"):
        # 构建文件路径模式
        file_pattern_path = os.path.join(folder_path, file_pattern)
        # 获取文件夹中的所有与路径模式匹配的文件
        matching_files = glob.glob(file_pattern_path)

        # 逐个加载文件的内容
        documents = []
        for matching_file in matching_files:
            file_path = os.path.join(folder_path, matching_file)
            with open(file_path, 'r', encoding='utf-8') as file:
                content = file.read()
                documents.append({'filename': matching_file, 'content': content})

        return documents